/**
 * 负责和消息数据打交道
*/
#pragma once

#include <iostream>
#include <mysql/mysql.h>

// 消息表
// CREATE TABLE `msg` (
//     `id` int(11) NOT NULL AUTO_INCREMENT,
//     `send_id` int(11) NOT NULL,
//     `recv_id` int(11) NOT NULL,
//     `kind` varchar(32),
//     `content` VARCHAR(255),
//     `status` varchar(32),
//     `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
//     PRIMARY KEY (`id`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

class MsgMgt
{

public:
    MsgMgt(){};
    ~MsgMgt(){};
    void insert_msg(int send_id, int recv_id, std::string kind, std::string content,std::string status);
    void mysql_my_init(MYSQL* &mysql);
    std::vector<std::vector<std::string>> get_msg_with_recv_id(int recv_id);
    std::vector<std::vector<std::string>> get_msg(int send_id, int recv_id, std::string kind, std::string status);

};

void MsgMgt::mysql_my_init(MYSQL* &mysql)
{
    mysql = mysql_init(nullptr); // mysql 可以被称为“句柄”
    if(mysql == nullptr)
        std::cerr << "mysql_init(nullptr) return null" << std::endl;
    if(mysql_real_connect(mysql,"127.0.0.1","qbt","134256","WSChat",
    8099,nullptr,0) == nullptr)
    {
        std::cerr << "数据库连接失败!!!" << std::endl;
        mysql_close(mysql);;
    }
    else
        mysql_set_character_set(mysql,"utf8");
}

void MsgMgt::insert_msg(int send_id, int recv_id, std::string kind, std::string content,std::string status)
{
    // 参数: 发送id 接收id 类型 内容 状态
    MYSQL* mysql = mysql_init(nullptr); // mysql 可以被称为“句柄”
    if(mysql == nullptr)
        std::cerr << "mysql_init(nullptr) return null" << std::endl;
    if(mysql_real_connect(mysql,"127.0.0.1","qbt","134256","WSChat",
    8099,nullptr,0) == nullptr)
    {
        mysql_close(mysql);
        return;
    }
    else
        mysql_set_character_set(mysql,"utf8");


    std::string insert_query = 
    "INSERT INTO msg (send_id, recv_id, kind, content, status) VALUES ('" +
    std::to_string(send_id) + "', '" + std::to_string(recv_id) + "', '" + 
    kind + "', '" + content + "', '" + status + "')";

    // 执行插入语句
    int ret = mysql_query(mysql, insert_query.c_str());

    if (ret != 0)
    {
        std::cerr << "MsgMgt::insert_msg插入数据失败: " << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return;
    }

    mysql_close(mysql);

    std::cout << "添加好友消息被存入数据库" << send_id << recv_id << std::endl;
    std::cout << kind << content << status << std::endl;
}

std::vector<std::vector<std::string>> MsgMgt::get_msg_with_recv_id(int recv_id)
{
    //std::cout << "MsgMgt::get_msg_with_recv_id 1" << recv_id << std::endl;

    MYSQL* mysql;
    MYSQL_ROW row;
    std::vector<std::vector<std::string>> msg_vv;
    std::vector<std::string> row_v;

    mysql_my_init(mysql);
    std::string insert_query = 
    "SELECT * FROM msg WHERE recv_id = " + std::to_string(recv_id) + " AND status = 'undelivered'";
    int ret = mysql_query(mysql, insert_query.c_str());
    if (ret != 0)
    {
        std::cerr << "MsgMgt::get_msg_with_recv_id 失败 1" << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return msg_vv;
    }

    MYSQL_RES *res = mysql_use_result(mysql);
    if (res == nullptr) {
        std::cerr << "MsgMgt::get_msg_with_recv_id 失败 2" << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return msg_vv;
    }

    while ((row = mysql_fetch_row(res)) != NULL) 
    {
        row_v.push_back(row[0]);
        row_v.push_back(row[1]);
        row_v.push_back(row[2]);
        row_v.push_back(row[3]);
        row_v.push_back(row[4]);
        row_v.push_back(row[5]);
        row_v.push_back(row[6]);
        msg_vv.push_back(row_v);
        row_v.clear();
    }

    mysql_free_result(res);
    mysql_close(mysql);

    return msg_vv;    
}


std::vector<std::vector<std::string>> MsgMgt::get_msg(int send_id, int recv_id, std::string kind, std::string status)
{
    MYSQL* mysql;
    MYSQL_ROW row;
    std::vector<std::vector<std::string>> msg_vv;
    std::vector<std::string> row_v;
    mysql_my_init(mysql);

    std::string query;
    if(send_id == -1)
    {
        query = 
        "SELECT * FROM msg WHERE recv_id = " + std::to_string(recv_id)
        + " AND kind = '" + kind + "'"
        + " AND status = '" + status + "'";
    }
    else
    {
        query = 
        "SELECT * FROM msg WHERE send_id = " + std::to_string(send_id) 
        + " AND recv_id = " + std::to_string(recv_id)
        + " AND kind = '" + kind + "'"
        + " AND status = '" + status + "'";
        //std::cout << "消息管理get_msg " << query << std::cout;
    }
    

    int ret = mysql_query(mysql, query.c_str());
    if (ret != 0)
    {
        std::cerr << "MsgMgt::get_msg 失败 1" << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return msg_vv;
    }

    MYSQL_RES *res = mysql_use_result(mysql);
    if (res == nullptr) {
        std::cerr << "MsgMgt::get_msg 失败 2" << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return msg_vv;
    }

    while ((row = mysql_fetch_row(res)) != nullptr) 
    {
        row_v.push_back(row[0]);
        row_v.push_back(row[1]);
        row_v.push_back(row[2]);
        row_v.push_back(row[3]);
        row_v.push_back(row[4]);
        row_v.push_back(row[5]);
        row_v.push_back(row[6]);
        msg_vv.push_back(row_v);
        row_v.clear();
    }

    mysql_free_result(res);
    mysql_close(mysql);
    return msg_vv;   
}